
****************************************************************************
**Win or Lose: Residential Sorting After a School Choice Lottery
**By Andrew Bibler & Stephen B Billings
****************************************************************************
	
cap log close
set more off

use sixsample-lottery-movers-analysis-cbgs.dta , clear

keep if stayer==1

gen notmove1 = (1 - move1)

gen move1_won = wonfirst*move1
gen notmove1_won = wonfirst*notmove1

gen move1_lost = (1 - wonfirst)*move1

gen notmove1_lost = notmove1*(1 - wonfirst)

gen allcbg_avail = ( x_app!=. & x_att!=. )

keep if allcbg_avail==1


** distances

gen dist_moved = sqrt((x_att - x_app)^2 + (y_att - y_app)^2)

gen dist_appch1 = sqrt((x_appch1 - x_app)^2 + (y_appch1 - y_app)^2)

gen dist_assschl = sqrt((x_assschl - x_app)^2 + (y_assschl - y_app)^2)  

gen dist_attschl = sqrt((x_attend - x_att)^2 + (y_attend - y_att)^2)

** initial CBG and future school
gen dist_app_attschl = sqrt((x_attend - x_app)^2 + (y_attend - y_app)^2)

** CBG to first school
gen dist_newres_as = sqrt((x_assschl - x_att)^2 + (y_assschl - y_att)^2)  

**
gen dist_moved_km = ( dist_moved / 3280.84 )

gen dist_appch1_km = ( dist_appch1 / 3280.84 )

gen dist_assschl_km = ( dist_assschl / 3280.84 )

gen dist_attschl_km = ( dist_attschl / 3280.84 )

gen dist_app_attschl_km = ( dist_app_attschl / 3280.84 )

gen dist_newres_as_km = ( dist_newres_as / 3280.84 )


local b = 6
local r = 0.125

levelsof(homeschl) , local(homeschools)

gen hsmathavg = . 
gen hsreadavg = . 

foreach s of local homeschools {

	su mathstand_as if assschl==`s'
	replace hsmathavg = r(mean) if homeschl==`s'
	
	su readstand_as if assschl==`s'
	replace hsreadavg = r(mean) if homeschl==`s'
	
	}		
	
levelsof(appch1schl) , local(appschools)

gen appschlmathavg = . 
gen appschlreadavg = . 

foreach s of local appschools {

	su mathstand_as if assschl==`s'
	replace appschlmathavg = r(mean) if appch1school==`s'
	
	su readstand_as if assschl==`s'
	replace appschlreadavg = r(mean) if appch1school==`s'
	
	}		
	
gen hsmathreadavg = (0.5*hsmathavg + 0.5*hsreadavg)
gen appschlmathreadavg = (0.5*appschlmathavg + 0.5*appschlreadavg)

gen math_diff_hs_app = appschlmathavg - hsmathavg
gen read_diff_hs_app = appschlreadavg - hsreadavg

gen mard_diff_hs_app = appschlmathreadavg - hsmathreadavg

su hsmathreadavg , detail
gen highquality = ( hsmathreadavg >= r(p50) )


**** FIGURES


set scheme s1mono

** FIGURE A1
kdensity dist_appch1_km if wonfirst==1 , b(`b') gen(distapp_w distapp_w_den)
kdensity dist_appch1_km if wonfirst==0 , b(`b') gen(distapp_l distapp_l_den)

twoway line distapp_w_den distapp_w , lc(navy) lp(solid) lw(medthick) ysc(range(`r')) || ///
		line distapp_l_den distapp_l , lc(red) lp(dash) lw(medthick) , ///
		ytitle(Density) ylabel(0(0.02)0.12) xtitle("Distance to First Choice School (km)") ///
		legend(label(1 "Winner") label(2 "Loser") cols(1)) ///
		saving("figureA1.gph" , replace) 


kdensity dist_app_attschl_km if move1_won==1 , b(`b') gen(dist_ap_at_mw dist_ap_at_mw_den)
kdensity dist_app_attschl_km if move1_lost==1 , b(`b') gen(dist_ap_at_ml dist_ap_at_ml_den)
kdensity dist_attschl_km if move1_won==1 , b(`b') gen(distatt_mw distatt_mw_den)
kdensity dist_attschl_km if move1_lost==1 , b(`b') gen(distatt_ml distatt_ml_den)

** FIGURE 2
twoway line dist_ap_at_mw_den dist_ap_at_mw , lc(navy) lp(solid) lw(medthick) ysc(range(`r')) || ///
		line dist_ap_at_ml_den dist_ap_at_ml , lc(red) lp(dash) lw(medthick) || ///
		line distatt_mw_den distatt_mw , lc(navy) lp(longdash) lw(medthick) || ///
		line distatt_ml_den distatt_ml , lc(red) lp("-..") lw(medthick) m(x) mc(red) ///
		ytitle(Density) ylabel(0(0.02)0.12) xtitle("Distance to Attended School (km)") ///
		legend(label(1 "Win-App Year") label(2 "Lost-App Year") label(3 "Win-Attend Year") label(4 "Lost-Attend Year")) ///
		saving("figure2.gph" , replace) 

	
		
** SCATTER PLOTS

global predictprobs "lunch?? lunch_miss anyt109 anyt110 anyt111 anyt112 anyt1lunch?? esly?? math_gr_lag read_gr_lag mardgr_int_lag science_gr_lag allgr_int_lag i.lowread_nonmag#i.year lep_Y_lag ma_aig_lag rd_aig_lag aig_int_lag"

logit wonfirst i.year i.appch1 $predictprobs 
predict probwin , pr
su probwin

bysort blockgroup_app appch1school homeschl: egen num_in_group = count(mastid)	

collapse (mean) mathstand_as readstand_as wonfirst hsmathavg hsreadavg hsmathreadavg appschlmathavg appschlreadavg appschlmathreadavg ///
				math_diff_hs_app read_diff_hs_app mard_diff_hs_app dist_appch1_km num_in_group probwin highquality , by(blockgroup_app appch1school homeschl)
			
** FIGURE A2			
twoway scatter dist_appch1_km mard_diff_hs_app [w=num_in_group] if probwin>=0.5 , m(Oh) mc(navy) || ///
		lfit dist_appch1_km mard_diff_hs_app[w=num_in_group] if probwin>=0.5 , ///
		ytitle("Distance to First Choice School (km)") xtitle("Difference in Average Test Scores") ///
		legend(label(1 "CBG - HS - First Choice Observations") label(2 "Fitted Line") cols(1)) ///	
		saving("figureA2.gph" , replace)

** FIGURE A3		
twoway scatter dist_appch1_km mard_diff_hs_app [w=num_in_group] if probwin<0.5 , m(Oh) mc(navy) || ///
		lfit dist_appch1_km mard_diff_hs_app[w=num_in_group] if probwin<0.5 , ///
		ytitle("Distance to First Choice School (km)") xtitle("Difference in Average Test Scores") ///
		legend(label(1 "CBG - HS - First Choice Observations") label(2 "Fitted Line") cols(1)) ///	
		saving("figureA3.gph" , replace)
		
		


